package com.IntelligenceInsight.Myth.mapper;

import com.IntelligenceInsight.Myth.model.Model;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
@Repository
public interface ModelMapper {
    @Select("SELECT * FROM model WHERE owner=#{owner}  GROUP BY name ORDER BY id DESC")
    List<Model> ModelListByOwner(@Param("owner") String owner);

    @Select("SELECT * FROM model ORDER BY timeCreate DESC LIMIT #{page},#{limit}")
    List<Model> ModelListAll(@Param("page") int page, @Param("limit") int limit);

    @Select("SELECT * FROM model ORDER BY id DESC")
    List<Model> ModelListAllforName();


    @Select("SELECT COUNT(*) FROM model")
    int ModelListAllCount();

    @Select("SELECT * FROM model WHERE uuid=#{uuid}")
    Model ModelByUuid(@Param("uuid") String uuid);

    @Select("SELECT * FROM model WHERE name=#{name} ORDER BY id DESC LIMIT 1")
    Model ModelByName(@Param("name") String name);

    @Insert("INSERT INTO model(uuid, name, owner, description, parentName) VALUES(#{uuid}, #{name}, #{owner}, #{description}, #{name})")
    int ModelAdd(@Param("uuid") String uuid, @Param("name") String name, @Param("owner") String owner,@Param("description") String description);

    @Insert("INSERT INTO model(uuid, name, owner, lastVersion, description, modelString) VALUES(#{uuid}, #{name}, " +
            "#{owner}, #{lastVersion}, #{description}, #{modelString})")
    int ModelClone(@Param("uuid") String uuid, @Param("name") String name, @Param("owner") String owner, @Param("lastVersion") int lastVersion, @Param("description") String description, @Param("modelString") String modelString);

    @Update("UPDATE model set lastVersion=#{lastVersion}, version=#{version}, modelString=#{modelString} WHERE uuid=#{uuid}")
    int ModelUpdate(@Param("uuid") String uuid, @Param("lastVersion") int lastVersion, @Param("version") int version, @Param("modelString") String modelString);

    @Delete("DELETE FROM model WHERE name=#{name}")
    int ModelRemoveByName(@Param("name") String name);
}
